package com.basker.pisces.orm.event;

import java.util.List;

import org.springframework.context.ApplicationEvent;
import org.springframework.util.CollectionUtils;

/**
 * 对象写入事件
 *
 * @author hangwen
 */
public class DataObjectWriteEvent extends ApplicationEvent {

    private static final long serialVersionUID = 2171960386985356409L;
    private List<DataRowWriteEvent> rowWriteEvents;

    public DataObjectWriteEvent(Object source, List<DataRowWriteEvent> rowWriteEvents) {
        super(source);
        this.rowWriteEvents = rowWriteEvents;
        this.resolveTimestamp();
    }

    /**
     * 获取所有行写入事件
     *
     * @return
     */
    public List<DataRowWriteEvent> getRowWriteEvents() {
        return rowWriteEvents;
    }

    private void resolveTimestamp() {
        if (CollectionUtils.isEmpty(rowWriteEvents)) {
            return;
        }

        Long timestamp = this.getTimestamp();
        rowWriteEvents.stream().forEach(e -> e.setTimestamp(timestamp));
    }

    /**
     * 数据行写入事件
     *
     * @author hangwen
     */
    public static class DataRowWriteEvent {
        private String tableName;
        private Object id;
        private WriteAction action;
        private List<FieldChangeEvent> fieldChangeEvents;
        private Long timestamp;

        public DataRowWriteEvent(String tableName, Object id, WriteAction action) {
            this(tableName, id, action, null);
        }

        public DataRowWriteEvent(String tableName, Object id, WriteAction action,
                                 List<FieldChangeEvent> fieldChangeEvents) {
            this.tableName = tableName;
            this.id = id;
            this.action = action;
            this.fieldChangeEvents = fieldChangeEvents;
        }

        /**
         * 写行为
         *
         * @return
         */
        public WriteAction getAction() {
            return action;
        }

        /**
         * 对于更新行为，获取字段更新事件
         *
         * @return
         */
        public List<FieldChangeEvent> getFieldChangeEvents() {
            return fieldChangeEvents;
        }

        /**
         * 数据行的id
         *
         * @return
         */
        public Object getId() {
            return id;
        }

        /**
         * 数据表
         *
         * @return
         */
        public String getTableName() {
            return tableName;
        }

        /**
         * 事件的时间戳
         *
         * @return
         */
        public Long getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(Long timestamp) {
            this.timestamp = timestamp;
        }

    }

    /**
     * 字段更新事件
     *
     * @author hangwen
     */
    public static class FieldChangeEvent {
        private String fieldName;
        private Object newValue;
        private Object oldValue;

        public FieldChangeEvent(String fieldName, Object newValue, Object oldValue) {
            super();
            this.fieldName = fieldName;
            this.newValue = newValue;
            this.oldValue = oldValue;
        }

        /**
         * 字段名(数据库字段)
         *
         * @return
         */
        public String getFieldName() {
            return fieldName;
        }

        /**
         * 获取更新后的值
         *
         * @return
         */
        public Object getNewValue() {
            return newValue;
        }

        /**
         * 获取之前的值
         *
         * @return
         */
        public Object getOldValue() {
            return oldValue;
        }

    }

    /**
     * 数据写入行为
     *
     * @author hangwen
     */
    public static enum WriteAction {
        Insert, Update, Delete
    }

}
